<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-09-17
 * Time: 14:38
 */

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

class Goods extends BaseModel
{
    use SoftDeletes;

    /**
     * 与模型关联的数据表
     *
     * @var string
     */
    protected $table = 'goods';

    /**
     * 主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * @var bool
     */
    public $timestamps = false;

    /**
     * @var array
     */
    protected $appends = [
        'logo_attachment',  //列表logo附件
        'head_image_attachment', //展示图组附件
//        'is_get_attachment',//通过设置此虚拟字段来判断是否加载 附件
    ];

    /**
     * 相关库存
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function goodsSku(){
        return $this->hasMany(GoodsSku::class, 'goodsId', 'id');
    }

    /**
     * 商品所属分类
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function category(){
        return $this->belongsTo(GoodsCategory::class, 'categoryId', 'categoryId');
    }

    /**
     * 拍卖信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function auctionGoods(){
        return $this->hasOne(AuctionGoodsInfo::class, 'goodsId', 'id');
    }

    /**
     * 返回列表logo文件全路径
     * @return bool
     */
    public function getLogoAttachmentAttribute()
    {
        $result = Attachment::find($this->logo);
        return $result ? $result->full_path : null;
    }

    /**
     * 返回列表logo文件全路径
     * @return array
     */
    public function getHeadImageAttachmentAttribute()
    {
        $result = [];
        $headImage = \GuzzleHttp\json_decode($this->headImage);
        if(is_array($headImage)){
            foreach ($headImage as $item){
                if($full = Attachment::find($item)){
                    $result[] = $full->full_path;
                }
            }
        }
        return $result ?: null;
    }

}
